<template>
  <div class="swiper-container" ref="swiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in banner" :key="item.id">
        <img :src="item.imgUrl" alt="">
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
export default {
  // 定义属性
  name: "Carsousel",
  data() {
    return {};
  },
  props: ["banner"],
  watch: {
    banner: {
      immediate: true,
      handler() {
        if (this.banner.length === 0) return;
        this.$nextTick(() => {
          new Swiper(this.$refs.swiper, {
            loop: true,
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
              type: "bullets",
            },
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    },
  },
};
</script>